package io.adrainty.bolee.trade.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.adrainty.bolee.trade.mapper.ITradeMapper;
import io.adrainty.bolee.trade.models.bo.TradeBO;
import io.adrainty.bolee.trade.models.entity.TradeEntity;
import io.adrainty.bolee.trade.repository.ITradeRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/31 下午11:40
 */

@Repository
public class TradeRepositoryImpl implements ITradeRepository {

    @Resource
    private ITradeMapper tradeMapper;

    @Override
    public TradeEntity findTradByProductOrderNo(Long productOrderNo) {
        Wrapper<TradeBO> wrapper = Wrappers.lambdaQuery(TradeBO.class)
                .eq(TradeBO::getProductOrderNo, productOrderNo)
                .orderByDesc(TradeBO::getCreatedTime)
                .last("limit 1");
        TradeBO tradeBO = tradeMapper.selectOne(wrapper);
        return BeanUtil.toBean(tradeBO, TradeEntity.class);
    }

    @Override
    public void save(TradeEntity tradeVO) {
        TradeBO tradeBO = BeanUtil.toBean(tradeVO, TradeBO.class);
        tradeMapper.insert(tradeBO);
        BeanUtil.copyProperties(tradeBO, tradeVO);
    }

    @Override
    public void updateTradeById(TradeEntity tradeEntity) {
        TradeBO tradeBO = BeanUtil.toBean(tradeEntity, TradeBO.class);
        tradeMapper.updateById(tradeBO);
        BeanUtil.copyProperties(tradeBO, tradeEntity);
    }
}
